Xceed DataGrid for WPF v7.2 Documentation
Deleting Selected Items

The following example demonstrates how to delete the selected items and handle the DeletingSelectedItemError and DeletingSelectedItems events.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">

  <Grid.Resources>

     <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"

                                        Source="{Binding Source={x:Static Application.Current}, Path=Orders}" />      

  </Grid.Resources>

   <xcdg:DataGridControl x:Name="OrdersGrid"

                         ItemsSource="{Binding Source={StaticResource cvs_orders}}"

                         IsDeleteCommandEnabled="True"

                         DeletingSelectedItemError="OrdersGrid_DeletingSelectedItemError"

                         DeletingSelectedItems="OrdersGrid_DeletingSelectedItems"/>       

</Grid>
VB.NET
Copy Code
Private Sub OrdersGrid_DeletingSelectedItemError( ByVal sender As Object, ByVal e As DeletingSelectedItemErrorRoutedEventArgs )

  Dim result As MessageBoxResult = System.Windows.MessageBox.Show( "The following error occurred while attempting to delete an item: " & _

                                            e.Exception.Message & " Do you want to attempt to continue?", "Error", MessageBoxButton.YesNoCancel )

  ' If "No", the item would be skipped. Since "Skip" is the default action,

  ' there is no need to verify it.

  If result = MessageBoxResult.Yes Then

    If Me.OrdersGrid.IsBeingEdited Then

      Try

        Me.OrdersGrid.CancelEdit()

        e.Action = DeletingSelectedItemErrorAction.Retry

      Catch

        e.Action = DeletingSelectedItemErrorAction.Skip

      End Try

    End If

  End If

  If result = MessageBoxResult.Cancel Then

    e.Action = DeletingSelectedItemErrorAction.Abort

  End If

End Sub

Private Sub OrdersGrid_DeletingSelectedItems( ByVal sender As Object, ByVal e As CancelRoutedEventArgs )

  Dim result As MessageBoxResult = System.Windows.MessageBox.Show( "Are you certain you want to delete the selected rows?", _

                                                                 "Confirm Delete", MessageBoxButton.YesNo )

  If result = MessageBoxResult.No Then

    e.Cancel = True

  End If

End Sub
C#
Copy Code
private void OrdersGrid_DeletingSelectedItemError( object sender, DeletingSelectedItemErrorRoutedEventArgs e )

{    

 MessageBoxResult result = System.Windows.MessageBox.Show( "The following error occurred while attempting to delete an item: " +

                                           e.Exception.Message + " Do you want to attempt to continue?", "Error", MessageBoxButton.YesNoCancel );

 // If "No", the item would be skipped. Since "Skip" is the default action,

 // there is no need to verify it.

 if( result == MessageBoxResult.Yes )

 {

   if( this.OrdersGrid.IsBeingEdited )

   {

     try

     {

       this.OrdersGrid.CancelEdit();

       e.Action = DeletingSelectedItemErrorAction.Retry;

     }

     catch

     {

       e.Action = DeletingSelectedItemErrorAction.Skip;

     }        

   }

 }

 if( result == MessageBoxResult.Cancel )

 {

   e.Action = DeletingSelectedItemErrorAction.Abort;

 }

}

private void OrdersGrid_DeletingSelectedItems( object sender, CancelRoutedEventArgs e )

{

 MessageBoxResult result = System.Windows.MessageBox.Show( "Are you certain you want to delete the selected rows?", "Confirm Delete", MessageBoxButton.YesNo );

 if( result == MessageBoxResult.No )

 {

   e.Cancel = true;

 }     

}